import { ExecutionContext, UnauthorizedException } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

export class LocalAuthGuard extends AuthGuard('local') {
  canActivate(context: ExecutionContext) {
    const user = super.canActivate(context);
    const request = context.switchToHttp().getRequest();
    const req_code = request.body.code ? request.body.code.toLowerCase() : '';
    const session_code = request.session.code
      ? request.session.code.toLowerCase()
      : '1234';
    const codeBool = session_code == req_code;
    if (user && codeBool) {
      return user;
    }
    throw new UnauthorizedException('验证码错误');
  }
}
